# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
pkgname=spamassassin
_pkgreal=Mail-SpamAssassin
pkgver=4.0.1
pkgrel=1
pkgdesc="The Powerful #1 Open-Source Spam Filter"
url="https://metacpan.org/pod/Mail::SpamAssassin"
arch="all"
license="Apache-2.0"
_perl_depends="perl-html-parser perl-digest-sha1 perl-netaddr-ip perl-net-dns perl-mail-dkim
	perl-mime-base64 perl-time-hires perl-libwww perl gpg"
depends="perl-mail-$pkgname curl"
makedepends="perl-dev $_perl_depends re2c"
subpackages="
	$pkgname-doc
	$pkgname-client
	$pkgname-compiler
	$pkgname-openrc
	perl-mail-$pkgname:cpan:noarch
	"
source="https://cpan.metacpan.org/authors/id/S/SI/SIDNEY/Mail-SpamAssassin-$pkgver.tar.gz
	spamd.initd
	spamd.confd
	spamd.crond
	Mail-SpamAssassin.patch
	fix-test.patch
	taint.patch
	"
builddir="$srcdir/$_pkgreal-$pkgver"

# secfixes:
#   3.4.5-r0:
#     - CVE-2020-1946
#   3.4.4-r0:
#     - CVE-2020-1930
#     - CVE-2020-1931
#   3.4.3-r0:
#     - CVE-2018-11805
#     - CVE-2019-12420
#   3.4.2-r0:
#     - CVE-2016-1238
#     - CVE-2017-15705
#     - CVE-2018-11780
#     - CVE-2018-11781

build() {
	rm t/dnsbl_subtests.t
	export CFLAGS="$CFLAGS $(perl -MConfig -E 'say $Config{ccflags}')"
	PERL_MM_USE_DEFAULT=1 perl -I. Makefile.PL \
		INSTALLDIRS=vendor \
		NO_PACKLIST=1 \
		NO_PERLLOCAL=1
	make
}

check() {
	make test
}

package() {
	make DESTDIR="$pkgdir" install
	install -Dm755 "$srcdir"/spamd.crond "$pkgdir"/etc/periodic/daily/$pkgname-update
	install -Dm755 "$srcdir"/spamd.initd "$pkgdir"/etc/init.d/spamd
	install -Dm644 "$srcdir"/spamd.confd "$pkgdir"/etc/conf.d/spamd
	mkdir -p "$pkgdir"/usr/sbin
	mv "$pkgdir"/usr/bin/spamd "$pkgdir"/usr/sbin/spamd
}

client() {
	depends=""
	pkgdesc="SpamAssassin client"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/spamc "$subpkgdir"/usr/bin
}

compiler() {
	pkgdesc="SpamAssassin rules compiler"
	depends="re2c gcc make libc-dev perl-dev perl-mail-$pkgname"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/sa-compile "$subpkgdir"/usr/bin/
}

cpan() {
	pkgdesc="SpamAssassin perl library"
	depends="$_perl_depends"
	mkdir -p "$subpkgdir"/usr "$subpkgdir"/etc "$subpkgdir"/var/lib/$pkgname
	mv "$pkgdir"/etc/mail "$subpkgdir"/etc
	mv "$pkgdir"/usr/share "$subpkgdir"/usr
	mv "$subpkgdir"/usr/share/$pkgname/user_prefs.template \
		"$subpkgdir"/etc/mail/$pkgname/user_prefs
	sed -i '/^#\*/d' "$subpkgdir"/etc/mail/$pkgname/user_prefs
}

sha512sums="
7ac2d789d8744dfe37f647013871e293de50cfcd792029956eb6cea8e51343aad135398bd91867c3c21a68e5fb6330bd6b38a04b794a24449a59287b46d4ac70  Mail-SpamAssassin-4.0.1.tar.gz
0a22933290a3abd147689bf3a9de4b6b277628c22966f353c5da932cd98560babf1d0bb9d92c456ea24decfb5af0bbc960192d29a90d9cab437e7986c75c8278  spamd.initd
274d3aa0d9aab05e83c8d5ad3e93a457649360021a67c8cb19088365bed681ebe26889cfa86f8c46a6044c7ee969231f2a71e3227adf8ad9e38d0286b9caf48d  spamd.confd
2ad52adfb6eb2ab28d59311853572ef4c5858e5b3cee1b8ff8c16f1d605e537e823746376bf468b6f041d953f24d5592940686024bc91153d5ec886d65fd16b3  spamd.crond
66a6daa7e9898864497e7d8e17efc6094aabc5a9a94afbc7da08acf4cf2430fad2cb0128b8db4be637f5dcabe3fa9f03490f9d7cbba3c1cc02b7824f63cd0965  Mail-SpamAssassin.patch
e6d57356fdbeba0825c8b7d6700786661a4d691a2f9865881d7183424118fe82ccc5e7b6de10538c30519529c82af855e9d5a93f99b120b2e33ec88a98b69d11  fix-test.patch
e98072df43e8f620c8203683c3a003b770ebd11bc49a554c6675578d3ee52223a5287d9433caa2edd6f22008d49eaa043f520f19f71b35afc89ff251966a10dd  taint.patch
"
